#!/usr/bin/env python

import re
import sys
import numpy as np
from numpy.linalg import norm

def distance(v1, v2):
    return np.dot(v1, v2) / (norm(v1) * norm(v2))

def puncvector(str):
    """ return a vector (number_of_periods, number_of_commas)"""
    return np.array([str.count(','), str.count('.')])

def puncsim(str1, str2):
    """ score the similarity of the punctuation vectors of two strings """
    return distance(puncvector(str1), puncvector(str2))
    
def nums(str):
    """ return a list of the numbers in the text """
    return [int(match) for match in re.findall(r'\b[0-9]+', str)]

def numsim(str1, str2):
    """ number similarity score """
    nums1 = nums(str1)
    nums2 = nums(str2)
    allnums = nums1 + nums2
    allnums.sort()
    ar_allnums = np.array(allnums)
    ar_zeros1 = np.zeros(len(allnums), int)
    ar_zeros2 = np.zeros(len(allnums), int)

    for num in nums1:
        ar_zeros1[np.searchsorted(ar_allnums, num)] += 1
    for num in nums2:
        ar_zeros2[np.searchsorted(ar_allnums, num)] += 1
    #print(nums1)
    #print(nums2)
    #print(allnums)
    return distance(ar_zeros1, ar_zeros2)

def scorestr(txt1, txt2):
    numscore = numsim(txt1, txt2)
    puncscore = puncsim(txt1, txt2)
    return "\t".join(str(datum) for datum in [numscore, puncscore, (numscore + puncscore) / 2])

def main(args):
    if (len(args)) < 2:
        print("Give me two files as arguments")
        return 1
    print(scorestr(open(args[0]).read(), open(args[1]).read()))
    return 0

if __name__ == "__main__":
    exit(main(sys.argv[1:]))
